package com.shfl.servermanage.logic.api;

import com.shfl.servermanage.common.ApiResultHelper;
import com.shfl.servermanage.common.ApiResultSimpleHelper;
import com.shfl.servermanage.common.SystemConstant;
import com.shfl.servermanage.logic.model.request.EditServerBtRequest;
import com.shfl.servermanage.logic.model.request.EditServerShellRequest;
import com.shfl.servermanage.logic.model.request.TestServerShellRequest;
import com.shfl.servermanage.logic.model.response.ServerShellDetailResponse;
import com.shfl.servermanage.logic.service.SysServerShellService;
import com.shfl.servermanage.shell.RemoteConnectUtil;
import com.shfl.servermanage.shell.info.RemoteConnectInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/server/shell")
@Api(value = "shell服务器", description = "shell服务器")
public class SysServerShellController {

    @Autowired
    private SysServerShellService sysServerShellService;


    @GetMapping("detail/by/server")
    @ApiOperation(value = "宝塔服务详情", notes = "宝塔服务详情")
    public ApiResultSimpleHelper<ServerShellDetailResponse> detail(
            @ApiParam(name = "serverId", example = "1", value = "服务器Id", required = true)
            @RequestParam(value = "serverId") Long serverId
    ) {
        ServerShellDetailResponse serverShellDetailResponse = new ServerShellDetailResponse();
        sysServerShellService.findByServerId(serverShellDetailResponse, serverId);
        return new ApiResultSimpleHelper<>(SystemConstant.RE_SUCCESS_CODE, serverShellDetailResponse);
    }

    @PostMapping(value = "/test")
    @ApiOperation(value = "测试连接", notes = "测试连接")
    public ApiResultHelper test(
            @RequestBody TestServerShellRequest testServerShellRequest
    ) throws Exception {
        RemoteConnectInfo info = new RemoteConnectInfo(
                testServerShellRequest.getShellIp(),
                testServerShellRequest.getShellPort(),
                testServerShellRequest.getShellName(),
                testServerShellRequest.getShellPassword()
        );
        RemoteConnectUtil.sendShell(info, null, null);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑", notes = "编辑")
    public ApiResultHelper edit(
            @RequestBody EditServerShellRequest editServerShellRequest
    ) throws Exception {
        sysServerShellService.edit(editServerShellRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }
}
